package com.ugrow.ugrowcommunity.activity.seatReservation.adapter;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.ugrow.ugrowcommunity.R;
import com.ugrow.ugrowcommunity.activity.seatReservation.SelectedSeatActivity;
import com.ugrow.ugrowcommunity.activity.seatReservation.bean.ClassroomBean;
import com.ugrow.ugrowcommunity.data.TimeDate;

import java.util.ArrayList;

public class SeatRAdapter extends RecyclerView.Adapter<SeatRAdapter.ViewHolder> {

    private ArrayList<ClassroomBean.Data> lassroomList;
    private Context context;
    private String day, time;

    //定义图片地址
    private String[] imgs = {"http://82.157.173.219:9000/yinggu/bg0.jpeg", "http://82.157.173.219:9000/yinggu/bg1.jpeg"
            , "http://82.157.173.219:9000/yinggu/bg2.jpeg", "http://82.157.173.219:9000/yinggu/bg3.jpeg", "http://82.157.173.219:9000/yinggu/bg4.jpeg"
            , "http://82.157.173.219:9000/yinggu/bg5.jpeg", "http://82.157.173.219:9000/yinggu/bg6.jpeg"
            , "http://82.157.173.219:9000/yinggu/bg7.jpeg", "http://82.157.173.219:9000/yinggu/bg8.jpeg", "http://82.157.173.219:9000/yinggu/bg9.jpeg"
            , "http://82.157.173.219:9000/yinggu/bg10.jpeg", "http://82.157.173.219:9000/yinggu/bg11.jpeg", "http://82.157.173.219:9000/yinggu/bg12.jpeg"};

    public void setLassroomList(ArrayList<ClassroomBean.Data> lassroomList) {
        this.lassroomList = lassroomList;
    }

    public void setContext(Context context) {
        this.context = context;
    }

    public void setDay(String day) {
        this.day = day;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private final TextView tvClassroomName, tvNumberSeats;
        private final ImageView imgClassroom;
        private final LinearLayout layoutOnclick;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvClassroomName = itemView.findViewById(R.id.tv_classroomname);
            tvNumberSeats = itemView.findViewById(R.id.tv_numberseats);
            imgClassroom = itemView.findViewById(R.id.img_classroom);
            layoutOnclick = itemView.findViewById(R.id.layout_onclick);
        }

        public TextView getTvClassroomName() {
            return tvClassroomName;
        }

        public TextView getTvNumberSeats() {
            return tvNumberSeats;
        }

        public ImageView getImgClassroom() {
            return imgClassroom;
        }

        public LinearLayout getLayoutOnclick() {
            return layoutOnclick;
        }
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_seatreservation, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

        ClassroomBean.Data data = lassroomList.get(position);
        holder.getTvClassroomName().setText(data.getClassName() + " " + data.getClassId());
        holder.getTvNumberSeats().setText("总计" + data.getSeatCapacity() + " 空闲:" + data.getSeatLave());
        if (data.getSeatIDList() != null) {
            for (String s : data.getSeatIDList()) {
                if (s != null) {
                    Log.d("TAG", s);
                }

            }
        }
        //图书馆背景图片
        if(imgs.length>lassroomList.size()){
            Glide.with(context).load(imgs[position]).into(holder.getImgClassroom());
        }else {
            Glide.with(context).load(imgs[0]).into(holder.getImgClassroom());
        }


        //页面跳转
        holder.getLayoutOnclick().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putString("classroomName", data.getClassName());//教室名称
                bundle.putString("classroomID", data.getClassId());//教室ID
                bundle.putStringArray("list", data.getSeatIDList());//被占用座位
                String date = TimeDate.getYear() + "-" + day + " ";//格式转换
                bundle.putString("day", date);//开始日期
                bundle.putString("time", time);//预约开始开始时间
                SelectedSeatActivity.actionStart(context, bundle);


            }
        });
    }

    @Override
    public int getItemCount() {
        if (lassroomList == null) {
            return 0;
        }
        return lassroomList.size();
    }


}
